/******************************************************************************
 * Copyright (C) 2024 EternalChip, Inc.(Gmbh) or its affiliates.
 * 
 * All Rights Reserved.
 * 
 * @file bsp_led.c
 * 
 * @par dependencies 
 * - bsp_led.h
 * - stdio.h
 * - stdint.h
 * 
 * @author Zhang Yuying
 * 
 * @brief Provide the HAL APIs of Key and corresponding opetions.
 * 
 * Processing flow:
 * 
 * call directly.
 * 
 * @version V1.0 2025-08-21
 *
 * @note 1 tab == 4 spaces!
 * 
 *****************************************************************************/

#include "bsp_led.h"

//******************************** Defines **********************************//
//******************************** Thread Func **********************************//
osThreadId_t led_TaskHandle;
const osThreadAttr_t led_Task_attributes = {
  .name = "led_Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityHigh,
};
//******************************** Thread Func **********************************//

/************************* Queue Handler **************************************/
QueueHandle_t led_queue;
/************************* Queue Handler **************************************/
//******************************** Defines **********************************//


 led_status_t led_on_off(led_operation_t led_opertion)
 {
    led_status_t led_ret = LED_OK;

    if (LED_ON == led_opertion)
    {
        HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
    }

    if (LED_OFF == led_opertion)
    {
        HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
    }

    if (LED_TOGGLE == led_opertion)
    {
        HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    }


    return led_ret;
}


 void led_task_func(void *argument)
 {
    led_status_t led_ret          = LED_OK;
    led_operation_t led_value     = LED_ON;
    led_queue = xQueueCreate(10, sizeof( led_operation_t ));

    if (NULL == led_queue) {
        printf("Led Queue Create Failed\r\n");
    } else {
        printf("Led Queue Create Success\r\n");
    }
    osDelay(100);
    for(;;)
    {
        //printf("Hello World\r\n");
        if(NULL != led_queue)
        {
            // Receive a message on the created queue.  Block for 10 ticks if a
            // message is not immediately available.
            if(pdPASS == xQueueReceive( led_queue, &( led_value ), ( TickType_t ) portMAX_DELAY ))
            {
                // pcRxedMessage now points to the struct AMessage variable posted
                // by vATask.
                led_on_off(led_value);
                if (LED_ON == led_value) 
                {
                    printf("Received value on queue: LED ON\r\n");
                } 

                if (LED_OFF == led_value) 
                {
                    printf("Received value on queue: LED OFF\r\n");
                }

                if (LED_TOGGLE == led_value) 
                {
                    printf("Received value on queue: LED TOGGLE\r\n");
                }
                //printf("Received value on queue: %d\r\n", receive_value);
            }
        }
        
    }
 }



